﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TCPGB02
    {

        #region ==AUF===========
        public static readonly DependencyProperty AUFProperty = DependencyProperty.Register("AUF",
            typeof(string), typeof(TCPGB02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFChanged));
        public string AUF
        {
            get
            {
                return (string)GetValue(AUFProperty);
            }
            set
            {
                SetValue(AUFProperty, value);
            }
        }
        private static void OnAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TCPGB02;
            if (src != null)
            {
                src.AUF = (string)e.NewValue;

            }
        }
        #endregion
        #region ==dwGD===========
        public static readonly DependencyProperty dwGDProperty = DependencyProperty.Register("dwGD",
        typeof(DataRow), typeof(TCPGB02),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwGDChanged));

        public DataRow dwGD
        {
            get
            {
                return (DataRow)GetValue(dwGDProperty);
            }
            set
            {
                SetValue(dwGDProperty, value);
                //OnGDChg(value);

            }
        }
        private static void OdwGDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TCPGB02;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwGD = row;
            }
        }
        #endregion
        /// <summary>
        /// TCPGB02查询库存，整批留位操作
        /// </summary>
        public TCPGB02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGLLG();
            this.DataContext = MV;
            InitializeComponent();
            KEY = "H120602014";

        }
        private void txtLO_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Focus();
                QueryCommand(null, null);
            }
        }
        DataRow dwVKP = null;
        DataTable TBGAAUF;
        //在dwGD变化时，也就是当销售单查询时，重新初始化环境。读取销售单列表和GAAUF
       /* public void OnGDChg(DataRow _dwGD)
        {
            try
            {
                if (_dwGD == null)
                    return;
                if (_dwGD["GAAUFPOS"].ToString() != "001")
                    return;
                dwVKP = null;
                dwData = TMV.VKP.TBMain.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                dwData["VKPAUFKRS"] = _dwGD["GAAUFKRS"];
                dwData["VKPAUFLFD"] = _dwGD["GAAUFLFD"];
                dwData["VKPAUFPOS"] = _dwGD["GAAUFPOS"];
                SResult = TMV.VKP.QueryVKPOS(dwData, 1);
                if (SResult.Tables[0].Rows.Count == 0) return;
                dwVKP = SResult.Tables[0].Copy().Rows[0];

                SResult = ((VGAAUF)MV).QueryGAAUF(dwGD, 26);
                TBGAAUF = SResult.Tables[0].Clone();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }*/
        //获取箱子列表
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            dwData = TBMain.NewRow();
            dwData["GLLO"] = LO;
            dwData["GLLB"] = LB;
            dwData["GLLP"] = LP;
            dwData["GLLBEH"] = KEY;
            dwData["GLOWNER"] = ART;
            dwData["GLFILLER1"] = FARBE;
            try
            {
                SResult = ((VGLLG)MV).QueryGLLG(dwData, 9);
                MergeData(SResult, false);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnAllocate_Click(object sender, RoutedEventArgs e)
        { 
            foreach (var item in dlgMaster.SelectedItems)
            {
                if (!item.GetType().ToString().Contains("DataRowView")) continue;
                DataRow _dwGB = ((System.Data.DataRowView)item).Row;
                double FREE = Convert.ToDouble(_dwGB["QFREE"].ToString());
                if (FREE == 0) continue;
               // DataRow _dwGD = FindGD(_dwGB);
                //if (_dwGD == null) continue;
                ALLOCATEBBOX(dwGD, _dwGB);
            }
            QueryCommand(null, null);
            AUF = "";
            AUF = "K" + dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString();

        }

        //根据纱线编号，决定是更新当前行，还是创建新的销售单+需求
        /*public DataRow FindGD(DataRow _dwGB)
        {
            //首次使用，当前的GAAUF表进行初始化，将纱线编号与第一个箱子一致
            if (TBGAAUF == null)
                return null;
            DataRow _dwGD = null;
            if (TBGAAUF.Rows.Count == 0)
                _dwGD = TBGAAUF.Rows[0];//如果仅有一行，就需要更新这一行的纱线编号。
            _dwGD["GAART"] = _dwGB["GPGART"].ToString();
            _dwGD["GAVAR"] = _dwGB["GPGVAR"].ToString();
            _dwGD["GAFARBE"] = _dwGB["GPGFARBE"].ToString();
            TMV.VGD.UpdateGAAUF(dwGD, 24);

            filter = "GAART='" + _dwGB["GPGART"].ToString() + "' AND GAVAR='" + _dwGB["GPGVAR"].ToString() + "' AND GAFARBE='" + _dwGB["GPGFARBE"].ToString() + "'";
            rows = TBGAAUF.Select(filter);
            if (rows.Length > 0) //如果在当前GAAUF中找到了纱线编号相同的销售单行，则返回这一行
                return rows[0];
           //否则新建，这个地方，是今天的主要内容
            _dwGD = TBGAAUF.NewRow();
            _dwGD.ItemArray = dwGD.ItemArray;
            _dwGD["GAART"] = _dwGB["GPGART"].ToString();
            _dwGD["GAVAR"] = _dwGB["GPGVAR"].ToString();
            _dwGD["GAFARBE"] = _dwGB["GPGFARBE"].ToString();
            ADDVKP(_dwGD);//根据新的GD，建立VKPOS
            TBGAAUF.Rows.InsertAt(_dwGD, 0);
            TMV.VGD.InsertGAAUF(_dwGD, app.company);//插入GAAUF
            rows = TBGAAUF.Select(filter);
            if (rows.Length > 0) return rows[0];
            return null;
        }*/
       /// <summary>
        /// //根据新的GD，建立VKPOS
       /// </summary>
       /// <param name="_dwGD"></param>
       /* public void ADDVKP(DataRow _dwGD)
        {
            if (dwVKP == null)
                return;
            string pos = "002";
            SResult = TMV.VKP.QueryVKPOS(dwVKP, 20);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string value = row["VKPAUFPOS"].ToString();
                if (value.Trim() == "")
                    value = "0";
                pos = Helper.Right("000" + (Convert.ToInt16(value) + 1).ToString(), 3);
            }
            //上面是获取最大的销售单行号
            dwData = TMV.VKP.TBMain.NewRow();
            dwData.ItemArray = dwVKP.ItemArray; ;
            dwData["VKPART"] = _dwGD["GAART"].ToString().PadRight(24);
            dwData["VKPFARBE"] = _dwGD["GAFARBE"].ToString().PadRight(10);
            dwData["VKPAUFPOS"] = pos;
            _dwGD["GAAUFPOS"] = pos;//这个地方疆传入的GD，行号和VKP一致
            TMV.VKP.InsertVKPOS(dwData, app.company);//插入新
        }*/
        public void ALLOCATEBBOX(DataRow _dwGD, DataRow _dwGB)
        {
            try
            {
                if (_dwGD == null)
                {
                    MessageBox.Show("请选择销售单行！");
                    return;
                }

                if (_dwGB == null)
                {
                    MessageBox.Show("请选择要留位的记录!");
                    return;
                }
                string GLLBEHI = _dwGB["GLLBEHI"].ToString();
                string GPPARTIE = _dwGB["GPPARTIE"].ToString();
                string GPSPPART = _dwGB["GPSPPART"].ToString();
                string GPFBPART = _dwGB["GPFBPART"].ToString();
                double QFREE = Convert.ToDouble(_dwGB["QFREE"].ToString());
                double QLOCK = Convert.ToDouble(_dwGB["QLOCK"].ToString());
                double GAKG1 = Convert.ToDouble(_dwGD["GAKG1"].ToString());
                double GAKG3 = Convert.ToDouble(_dwGD["GAKG3"].ToString());
                #region===dwZUTEI========
                PM["ORDERNUM"] = _dwGD["GAAUFKRS"].ToString() + _dwGD["GAAUFLFD"].ToString() + _dwGD["GAAUFPOS"].ToString() + _dwGD["GAPOS"].ToString() + _dwGD["GASA"].ToString();
                PM["CART"] = _dwGB["GPGART"].ToString() + _dwGB["GPGVAR"].ToString() + _dwGB["GPGFARBE"].ToString();
                //PM["LPLBKZ"] = LPLBKZ;
                PM["GPBPARTIE"] = GPPARTIE;
                //PM["GPBPARTIEKZ"] = dwGB["GPBPARTIEKZ"];
                PM["LO"] = _dwGB["GLLO"];
                PM["LB"] = _dwGB["GLLB"];
                PM["GLLBEHI"] = GLLBEHI;
                PM["GPSPPART"] = _dwGB["GPSPPART"];
                PM["ZUEINTKG"] = QFREE * 1000;
                TMV.ZU.NEWVKZUTEI(PM);
                dwData =TMV.VGB.TBMain.NewRow();
                dwData["GPBPARTIE"] = _dwGB["GPPARTIE"];
                dwData["GPBPARTIEKZ"] = _dwGB["GPPARTIEKZ"];
                dwData["GPBLO"] = _dwGB["GLLO"];
                dwData["GPBLB"] = _dwGB["GLLB"];
                TMV.VGB.UpdateGPBST(dwData, 5);
                TMV.GU.UpdateGAAUF(_dwGD, 23);
                dwData = VVKPOS.Instance.TBMain.NewRow();
                dwData["VKPMANDANT"] = _dwGD["GAMANDANT"];
                dwData["VKPAUFKRS"] = _dwGD["GAAUFKRS"];
                dwData["VKPAUFLFD"] = _dwGD["GAAUFLFD"];
                dwData["VKPAUFPOS"] = _dwGD["GAAUFPOS"];
                TMV.VKP.UpdateVKPOS(dwData, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
                #endregion
        }
    }
}
